<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/3/3
 * Time: 15:43
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Bogoodsin extends Base
{
    /**
     * 进货列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $bo_number = trim(input('bo_number'), ' '); // 订单号
            $user_username = trim(input('user_username')); // 用户姓名
            $user_phone = trim(input('user_phone')); // 手机号
            $user_sheng = input('user_sheng'); // 省
            $user_shi = input('user_shi'); // 市
            $user_qu = input('user_qu'); // 区
            $start_time = input('start_time');
            $end_time = input('end_time');
            
            $where['bo_status'] = 4;
            if(isset($bo_number) && $bo_number != ''){
                $where['bo_number'] = ['like', '%' . $bo_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($user_sheng) && $user_sheng != ''){
                $where['user_sheng'] = $user_sheng;
            }
            if(isset($user_shi) && $user_shi != ''){
                $where['user_shi'] = $user_shi;
            }
            if(isset($user_qu) && $user_qu != ''){
                $where['user_qu'] = $user_qu;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bo_create_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bo_create_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bo_create_time'] = ['<=', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_order')
                ->join('up_u_user', 'bo_user_id = user_id', 'LEFT')
                ->join('up_area u1', 'u1.district_id = user_sheng', 'LEFT')
                ->join('up_area u2', 'u2.district_id = user_shi', 'LEFT')
                ->join('up_area u3', 'u3.district_id = user_qu', 'LEFT')
                ->where($where)
                ->count('bo_id');
            $lists = Db::table('up_bo_order')
                ->join('up_u_user', 'bo_user_id = user_id', 'LEFT')
                ->join('up_area u1', 'u1.district_id = user_sheng', 'LEFT')
                ->join('up_area u2', 'u2.district_id = user_shi', 'LEFT')
                ->join('up_area u3', 'u3.district_id = user_qu', 'LEFT')
                ->field('bo_id, user_username, user_phone, bo_number, bo_total_price, bo_total_number, bo_create_time, 
                u1.district sheng, u2.district shi, u3.district qu, user_shop_name')
                ->where($where)
                ->order('bo_create_time desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    
    /**
     * 商品列表
     * @return mixed|\think\response\Json
     */
    public function orderGoods(){
        if(Request::instance()->isAjax()){
            $where['boi_order_id'] = session('bo_id');
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_order_info')
                ->join('up_bo_goods', 'bg_id = boi_goods_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_bo_order_info')
                ->join('up_bo_goods', 'bg_id = boi_goods_id', 'LEFT')
                ->field('bg_name, bg_thumb, bg_price, boi_goods_number, boi_goods_price, bg_spec')
                ->where($where)
                ->order('boi_id asc')
                ->page($Nowpage,$limits)
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $bo_id = input('bo_id');
        if(isset($bo_id) && $bo_id != ''){
            session('bo_id', $bo_id);
        }
        return $this->fetch('');
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $order_ids = input('ids');

        $where['bo_status'] = 4;
        if($order_ids == -1){
            $bo_number = trim(input('bo_number'), ' '); // 订单号
            $user_username = trim(input('user_username')); // 用户姓名
            $user_phone = trim(input('user_phone')); // 手机号
            $user_sheng = input('user_sheng'); // 省
            $user_shi = input('user_shi'); // 市
            $user_qu = input('user_qu'); // 区
            $start_time = input('start_time');
            $end_time = input('end_time');

            if(isset($bo_number) && $bo_number != ''){
                $where['bo_number'] = ['like', '%' . $bo_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($user_sheng) && $user_sheng != ''){
                $where['user_sheng'] = $user_sheng;
            }
            if(isset($user_shi) && $user_shi != ''){
                $where['user_shi'] = $user_shi;
            }
            if(isset($user_qu) && $user_qu != ''){
                $where['user_qu'] = $user_qu;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bo_create_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bo_create_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bo_create_time'] = ['<=', $end_time];
            }
        }else{
            if($order_ids == ''){
                return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            }
            $where['bo_id'] = ['IN', $order_ids];
        }

        $lists = Db::table('up_bo_order')
            ->join('up_u_user', 'bo_user_id = user_id', 'LEFT')
            ->join('up_area u1', 'u1.district_id = user_sheng', 'LEFT')
            ->join('up_area u2', 'u2.district_id = user_shi', 'LEFT')
            ->join('up_area u3', 'u3.district_id = user_qu', 'LEFT')
            ->field('bo_id, bo_number, bo_total_price, bo_create_time, 
            user_username, user_phone, user_shop_name, u1.district sheng, u2.district shi, u3.district qu, 
            bo_total_number')
            ->where($where)
            ->order('bo_create_time desc')
            ->select();
        foreach($lists as $k => $v){
            // 下单人手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
            // 总金额
            $lists[$k]['bo_total_price'] = "\t" . $v['bo_total_price'] . "\t";
            // 商品
            $goods = Db::table('up_bo_order_info')
                ->join('up_bo_goods', 'boi_goods_id = bg_id', 'LEFT')
                ->where('boi_order_id', $v['bo_id'])
                ->field('bg_name, bg_spec, boi_goods_number')
                ->select();
            $str = '';
            foreach($goods as $kk => $vv){
                $str .= "{$vv['bg_name']}--{$vv['bg_spec']}--{$vv['boi_goods_number']}\n";
            }
            $lists[$k]['order_goods'] = rtrim($str, "\n");
        }
        $cellname = [
            ['bo_id', '订单ID', 10, 'LEFT'],
            ['bo_number', '订单编号', 25, 'LEFT'],
            ['order_goods', '商品', 30, 'LEFT'],
            ['bo_total_price', '订单总金额', 15, 'LEFT'],
            ['bo_total_number', '订单总数量', 15, 'LEFT'],
            ['user_username', '用户姓名', 15, 'LEFT'],
            ['user_phone', '用户手机号', 15, 'LEFT'],
            ['sheng', '省', 15, 'LEFT'],
            ['shi', '市', 15, 'LEFT'],
            ['qu', '区', 15, 'LEFT'],
            ['user_shop_name', '店铺名称', 15, 'LEFT'],
            ['bo_create_time', '进货时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('进货列表', 'Goodsin', $cellname, $lists);
        return json($res);
    }
}